//
//  API_Book.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/21.
//

import UIKit
import Moya

enum API_Book {
    /// 书籍详情
    case bookDetail(String)
    /// 收藏
    case collect(Int, String)
    /// 章节信息
    case chapterList(String)
    /// 章节内容
    case chapterContent(String, Int)
    /// 章节阅读
    case chapterRead(String, Int, String, Int)
    /// 章节完读
    case chapterReadComplete(String, Int, String)
}

let book_timeoutClosure = { (endpoint: Endpoint, done: @escaping MoyaProvider<API_Book>.RequestResultClosure) in
    do {
        var request = try endpoint.urlRequest()
        request.timeoutInterval = 5
        done(.success(request))
    }catch {
        return
    }
}

let provider_book = MoyaProvider<API_Book>(requestClosure: book_timeoutClosure)

extension API_Book: TargetType {
    var baseURL: URL {
        return URL(string: server.APIHost)!
    }
    
    var path: String {
        switch self {
        case .bookDetail(_):
            return "/Article/book"
        case .collect(_, _):
            return "/Article/userFavorite"
        case .chapterList(_):
            return "/Article/chapters"
        case .chapterContent(_, _):
            return "/Article/chapter"
        case .chapterRead(_, _, _, _):
            return "/Article/readChapter"
        case .chapterReadComplete(_, _, _):
            return "Uploaddata/updateUserReading"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var parameterEncoding: ParameterEncoding {
        return JSONEncoding.default
    }
    
    var sampleData: Data {
        return "test".data(using: String.Encoding.utf8)!
    }
    
    var task: Task {
        var ps: [String: Any] = ["ver": getAPPVersion(), "channel": "ios", "third_channel": "apple", "timestamp": Date().timeStamp, "userId": getUser()?.userId ?? "", "uuid": getKeyUUId(), "user_channel": getADID()]
        switch self {
        case .bookDetail(let bid):
            ps["bookId"] = bid
        case .collect(let col, let bid):
            ps["is_favorite"] = col
            ps["bookId"] = bid
        case .chapterList(let bid):
            ps["bookId"] = bid
        case .chapterContent(let bid, let chapterId):
            ps["bookId"] = bid
            ps["chapterId"] = chapterId
        case .chapterRead(let bid, let chapterId, let name, let location):
            ps["bookId"] = bid
            ps["chapterId"] = chapterId
            ps["chapterName"] = name
            ps["location"] = location
        case .chapterReadComplete(let bid, let chapterId, let name):
            ps["bookId"] = bid
            if chapterId == 0 {
                ps["chapterId"] = ""
            }else {
                ps["chapterId"] = chapterId
            }
            ps["chapterName"] = name
        }
        
        return .requestParameters(parameters: ps, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["content-type": "application/json; charset=UTF-8"]
    }
}
